package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.admin.mapper.BlogMapper;
import com.itheima.admin.mapper.CategoryMapper;
import com.itheima.admin.service.Blogservice;
import com.itheima.xo.pojo.Blog;
import com.itheima.xo.pojo.Category;
import com.itheima.xo.vo.BlogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.admin.service.impl
 * fileName :BlogServiceImpl
 * creationTime :2022-10-28 19:52
 */
@Service
@Slf4j
public class BlogServiceImpl implements Blogservice {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Boolean add(Blog blog) {
        Boolean legal = isLegal(blog);
        if (!legal){
            return false;
        }
        int insert = blogMapper.insert(blog);
        return insert > 0;
    }




    @Override
    public Boolean delete(String id) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getBlogId,id);
        lqw.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(lqw);
        if (blog==null){
            log.info("该ID“{}”不存在",id);
            return false;
        }
        blog.setDeleted(LocalDateTime.now());
        int update = blogMapper.updateById(blog);
        return update>0;
    }

    @Override
    public Boolean update(Blog blog) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getBlogId,blog.getBlogId());
        lqw.isNull(Blog::getDeleted);
        Blog blogInDB = blogMapper.selectOne(lqw);
        if (blogInDB==null){
            log.info("需要更新的ID“{}”不存在",blog.getBlogId());
            return false;
        }
        Boolean legal = isLegal(blog);
        if (!legal){
            return false;
        }
        if(!blog.getAuthor().equals(blogInDB.getAuthor())){
            log.info("不能修改作者");
            return false;
        }
        blog.setUpdateTime(LocalDateTime.now());
        int update = blogMapper.updateById(blog);
        return update>0;
    }

    @Override
    public List<BlogVo> findAll() {
        List<BlogVo> blogVos = new ArrayList<>();
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.isNull(Blog::getDeleted);
        lqw.orderByDesc(Blog::getCreateTime);
        List<Blog> blogs = blogMapper.selectList(lqw);
        for (Blog blog : blogs) {
            BlogVo vo = getBlogVo(blog);
            blogVos.add(vo);
        }
        return blogVos;
    }


    @Override
    public BlogVo findById(String id) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getBlogId, id);
        lqw.isNull(Blog::getDeleted);
        lqw.orderByDesc(Blog::getCreateTime);
        Blog blog = blogMapper.selectOne(lqw);
        if (blog == null) {
            return null;
        }
        BlogVo blogVo = getBlogVo(blog);
        return blogVo;
    }

    @Override
    public BlogVo findByTitle(String title) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getTitle, title);
        lqw.isNull(Blog::getDeleted);
        lqw.orderByDesc(Blog::getCreateTime);
        Blog blog = blogMapper.selectOne(lqw);
        if (blog == null) {
            return null;
        }
        BlogVo blogVo = getBlogVo(blog);
        return blogVo;
    }

    @Override
    public List<Blog> findByCategoryId(String categoryId) {
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getCategoryId, categoryId);
        lqw.isNull(Blog::getDeleted);
        lqw.orderByDesc(Blog::getCreateTime);
        return blogMapper.selectList(lqw);
    }

    @Override
    public Blog findPoById(String id) {
        LambdaQueryWrapper<Blog> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Blog::getBlogId,id);
        lqw.isNull(Blog::getDeleted);
        return blogMapper.selectOne(lqw);
    }

    private Boolean isLegal(Blog blog) {
        String title = blog.getTitle();
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Blog::getTitle, title);
        Blog blogInDB = blogMapper.selectOne(lqw);
        if (blogInDB!=null&&!blogInDB.getBlogId().equals(blog.getBlogId())) {
            log.info("标题“{}”已存在，不得有重复的标题", title);
            return false;
        }
        Category category = getCategory(blog.getCategoryId());
        if (category==null){
            log.info("该分类id“{}”不存在");
            return false;
        }
        return true;
    }



    private BlogVo getBlogVo(Blog blog) {
        Category category = getCategory(blog.getCategoryId());
//        log.info("{}", category);
        BlogVo vo = new BlogVo(blog.getBlogId(), category.getCategoryName(),
                blog.getTitle(),  blog.getAuthor(),
                blog.getCreateTime(), blog.getCreateTime());
        return vo;
    }

    private Category getCategory(String categoryId) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getCategoryId, categoryId);
        lqw.isNull(Category::getDeleted);
        Category category = categoryMapper.selectOne(lqw);
        return category;
    }
}
